<template>
    <div id="login">
        <h2 id="text">登录</h2>
        <el-form :model="loginForm" status-icon :rules="loginRules" ref="ruleForm"  label-width="80px" class="demo-ruleForm" id="loginForm">
            <el-form-item label="账号:" prop="username">
                <el-input type="text" v-model="loginForm.username" autocomplete="off" style="width: 300px"></el-input>
            </el-form-item>
            <el-form-item label="密码:" prop="password">
                <el-input type="password" v-model="loginForm.password" autocomplete="off" style="width: 300px"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="login(loginForm)" >登录</el-button>
                <el-button type="primary"  >取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {
        name: "Login",
        data(){
            return{
                loginForm:{username:"" , password:""},
                loginRules:{
                    username:[
                        { required: true, message: '请输入账号', trigger: 'blur' },
                    ],
                    password:[
                        { required: true, message: '请输入密码', trigger: 'blur' },
                    ]
                }
            }
        },
        methods:{
            login(loginForm){
                    this.$http.post("http://localhost:7000/login",this.loginForm).then(resp=>{
                        if(resp.data.code===2000){
                            //获取服务器的token值，保存local Storage
                            localStorage.setItem("token",resp.data.data);

                            //跳转路由
                            this.$router.push("/home")
                        }else {
                            this.$message.error("账号或密码错误")
                        }
                    })
            }
        }
    }
</script>

<style>
    #login{
        width: 450px;
        height: 300px;
        margin: auto auto;
        background-color: #dfeae8;
        opacity:0.8;
    }
    #text{
        padding-left: 10px ;
        padding-top: 40px;
        border-bottom: 3px solid #0b2e13;
    }
</style>